
#include "Component/LRG_DebugComponent.h"

#include "LineReplicationGraphObject.h"
#include "Net/UnrealNetwork.h"

ULRG_DebugComponent::ULRG_DebugComponent(const FObjectInitializer& ObjectInitializer):Super(ObjectInitializer)
{
	SetIsReplicatedByDefault(true);
}

void ULRG_DebugComponent::EnableDebug_Implementation(bool Value)
{
	if (auto LineReplicationGraph = ULineReplicationGraph::Get(this) )
	{
		if (Value)
		{
			LineReplicationGraph->AddDebugComponent(this);
		}
		else
		{
			LineReplicationGraph->RemoveDebugComponent(this);
		}
	}
}

void ULRG_DebugComponent::BeginPlay()
{
	Super::BeginPlay();

	if (bEnableDebug)
	{
		APlayerController* PC = Cast<APlayerController>(GetOwner());
		if (PC && PC->HasAuthority())
		{
			if (auto LineReplicationGraph = ULineReplicationGraph::Get(this) )
			{
				LineReplicationGraph->AddDebugComponent(this);
			}
		}
	}
}

void ULRG_DebugComponent::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
	Super::EndPlay(EndPlayReason);

	APlayerController* PC = Cast<APlayerController>(GetOwner());
	if (PC && PC->HasAuthority())
	{
		if (auto LineReplicationGraph = ULineReplicationGraph::Get(this) )
		{
			LineReplicationGraph->RemoveDebugComponent(this);
		}
	}
}

void ULRG_DebugComponent::GetLifetimeReplicatedProps(TArray<class FLifetimeProperty>& OutLifetimeProps) const
{
	Super::GetLifetimeReplicatedProps(OutLifetimeProps);

	DOREPLIFETIME_CONDITION( ULRG_DebugComponent, CurrentGridCoordX,	COND_None );
	DOREPLIFETIME_CONDITION( ULRG_DebugComponent, CurrentGridCoordY,	COND_None );
}
